import { isSame } from "@/utils/index";

const message = useMessage(); // 消息弹窗
export default function () {
	const pageParams = reactive({
		pageNo: 1,
		pageSize: 10
	});
	const popoverRef = ref(null);
	const currentRow = ref(null);
	// 打开popover
	const handleMixinPopover = () => {
		unref(popoverRef.value).popperRef?.delayHide?.();
	};
	/*
    公用方法-提交
    https: 方法
    params: 参数
    getList: 当前需要刷新的列表方法
    isTips: 是否出现提示
	tips: 提示词
  */
	const handleMixinSubmit = async (https, params, getList, isTips = false, tips = "提交") => {
		if (!params) return;
		if (isTips) {
			await message.delConfirm(`确认${tips}吗？`);
		}
		try {
			await https(params);
			message.success("操作成功");
			if (getList) {
				getList();
			}
		} catch (err) {
			return Promise.reject(`mixin错误====${err}`);
		}
	};
	// 鼠标移入
	const handleMixinFocus = (dataList, row) => {
		dataList.map((item) => {
			return (item.isEdit = item.id === row.id);
		});
		currentRow.value = { ...row };
	};
	// 鼠标移除  isEmpty是否可以为空
	const handleMixinBlur = async (https, params, paramsRow, getList, dataList, keys, isEmpty) => {
		if(!isEmpty){
			if (!params[keys]) return;
		}
		dataList.map((item) => {
			return (item.isEdit = false);
		});
		const same = isSame(Object.keys(paramsRow).length > 0 ? paramsRow : currentRow.value, params, "isEdit");
		console.log("是否一样===", same);
		if (same){
			return Promise.reject();
		};
		await handleMixinSubmit(https, params, getList, false);
		currentRow.value = { ...params };
	};
	const methods = {
		handleMixinSubmit,
		handleMixinPopover,
		handleMixinFocus,
		handleMixinBlur,
	};

	return {
		...pageParams,
		...methods,
		popoverRef
	};
}
